home *** CD-ROM | disk | FTP | other *** search
- /* OpenPreferenceFile.h */
- /*
- * OpenPreferenceFile.h
- * Copyright © 1993-94 Apple Computer Inc. All rights reserved.
- */
- #include <Errors.h>
- #include <GestaltEqu.h>
- #include <Folders.h>
- #include <Resources.h>
- #include <Types.h>
- #ifndef FALSE
- #define FALSE 0
- #define TRUE 1
- #endif
-
- OSErr OpenPreferenceFile(
- OSType prefFileType,
- OSType prefFileCreator,
- SignedByte permission,
- short *prefVRefNum,
- StringPtr prefFileName
- );
-
- static void ClearMemory(
- Ptr dataPtr,
- unsigned long dataSize
- )
- {
- while (dataSize-- > 0)
- *dataPtr++ = 0;
- }
- #define CLEAR(what) (ClearMemory((Ptr) &what, sizeof what))
-
- /*
- * Open the preference file. Return noErr if successful.
- * Note that the preference file is specified by its file type and creator, not by
- * the file name -- this is so that the user can re-name the file, or so it can
- * be localized without changing the program.
- *
- * This routine does not create the preference file.
- */
- OSErr
- OpenPreferenceFile(
- OSType prefFileType,
- OSType prefFileCreator,
- SignedByte permission,
- short *prefVRefNum, /* Result: resource file refNum */
- StringPtr prefFileName /* Result: resource file name */
- )
- {
- OSErr status;
- long gestaltResult;
- short prefFolderVRefNum;
- long prefFolderDirID;
- CInfoPBRec specPBRec;
- FSSpec prefFSSpec; /* IM-VI 25-15, 25-30 */
- short i;
- #define SPEC (specPBRec.hFileInfo)
-
- status = Gestalt(gestaltFindFolderAttr, &gestaltResult);
- if (status == noErr) { /* We have FindFolder */
- status = FindFolder(
- kOnSystemDisk,
- kPreferencesFolderType,
- kDontCreateFolder,
- &prefFolderVRefNum,
- &prefFolderDirID
- );
- }
- if (status == noErr) { /* We have a Preferences Folder */
- CLEAR(specPBRec);
- SPEC.ioNamePtr = prefFileName; /* This will get the file name */
- SPEC.ioVRefNum = prefFolderVRefNum;
- for (i = 1; status == noErr; i++) { /* Search for this creator/type */
- SPEC.ioDirID = prefFolderDirID;
- SPEC.ioFDirIndex = i;
- status = PBGetCatInfo(&specPBRec, FALSE);
- if (status == noErr
- && SPEC.ioFlFndrInfo.fdType == prefFileType
- && SPEC.ioFlFndrInfo.fdCreator == prefFileCreator)
- break; /* Successful: we found it */
- }
- }
- if (status == noErr) { /* We have the Preferences file */
- status = FSMakeFSSpec(
- prefFolderVRefNum,
- prefFolderDirID,
- prefFileName,
- &prefFSSpec
- );
- }
- if (status == noErr) { /* We have a pref file FSSpec */
- *prefVRefNum = FSpOpenResFile(&prefFSSpec, permission);
- status = ResError();
- }
- if (status != noErr)
- *prefVRefNum = 0;
- return (status);
- #undef SPEC
-
- }